重新調整遊戲首頁的腳本控制
開啟 SuperNewJeans-Home 場景,並建立一個名為 HomeManager 的腳本
➔ 開啟 HomeManager 腳本,輸入以下程式碼並存檔
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class HomeManager : MonoBehaviour
{
void Start()
{
}
void Update()
{
}
public void GameStart()
{
SceneManager.LoadScene("SuperNewJeans");
}
}
➔ 回到 Unity 介面,點擊 Game Manager 物件,並關閉 GameManager 腳本
➔ 將 HomeManager 腳本 拖曳至 Game Manager 物件中
➔ 將 Start 按鈕在 OnClick 欄位的腳本修改為 HomeManager.GameStart 的方式
HomeManager 腳本主要是用於控制 SuperNewJeans-Home 場景的操作
可以選擇關閉或刪除 GameManager 物件中的 GameManager 腳本
修改 Player 撞擊障礙物時的效果樣式
開啟 SuperNewJeans 場景 ➔ 開啟 PlayerController 腳本,將程式碼修改為以下版本並存檔
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
public class PlayerController : MonoBehaviour
{
public GameManager gm;
public float currentPosY = 0;
public float speed = 0;
void Start()
{
gm = GameObject.Find("GameManager").GetComponent<GameManager>();
currentPosY = 0.75f;
speed = 0.1f;
}
void Update()
{
}
private void FixedUpdate()
{
if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W))
{
currentPosY = currentPosY + speed;
if (currentPosY >= 5.3f)
{
currentPosY = 5.3f;
}
}
if (Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S))
{
currentPosY = currentPosY - speed;
if (currentPosY <= -3.3f)
{
currentPosY = -3.3f;
}
}
gameObject.transform.position = new Vector3(-5.5f, currentPosY, 0f);
}
private void OnTriggerEnter(Collider collision)
{
string type = collision.gameObject.tag;
GameObject targetObject = collision.gameObject;
if (type == "rabbit")
{
Debug.Log("hit rabbit");
gm.updateRabbitPoint();
Destroy(targetObject);
}
if (type == "cookie")
{
Debug.Log("hit cookie");
gm.updateCookiePower();
Destroy(targetObject);
}
if (type == "big-rabbit")
{
Debug.Log("hit big-rabbit");
}
// 會扣HP的種類
if (type == "floor")
{
Debug.Log("hit floor");
if (isInvincible) return; // 如果是無敵狀態,則不執行以下程式碼
gm.updateHP(-1);
damageEffect();
}
if (type == "enemy")
{
Debug.Log("hit enemy");
if (isInvincible) return; // 如果是無敵狀態,則不執行以下程式碼
gm.updateHP(-1);
damageEffect();
}
}
void damageEffect()
{
if (isInvincible == false)
{
isInvincible = true;
hidePlayer();
Invoke("initDamageEffect", 1.2f);
}
}
bool isInvincible = false;
int hideCounter = 0;
int hideTimes = 6;
void hidePlayer()
{
if (hideCounter < hideTimes)
{
hideCounter++;
transform.GetChild(0).gameObject.SetActive(false);
Invoke("showPlayer", 0.1f);
}
}
void showPlayer()
{
transform.GetChild(0).gameObject.SetActive(true);
Invoke("hidePlayer", 0.1f);
}
void initDamageEffect()
{
isInvincible = false;
hideCounter = 0;
}
}
示意圖為 PlayerController 腳本下半部修改後之程式碼
完成各階段 UI 畫面與流程
開啟 GameManager 腳本,將程式碼修改為以下版本並存檔
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using TMPro;
public class GameManager : MonoBehaviour
{
public GameObject player;
public int score = 0;
public float score_f = 0;
public int hp = 3;
public int cookiePower = 0;
public int highScore = 0;
public int rabbitPoint = 0;
public bool isGameOver = false; // 新增遊戲結束的布林值變數
public List<GameObject> steps = new List<GameObject>();
// UI
public TextMeshProUGUI hpUI;
public TextMeshProUGUI rabbitUI;
public TextMeshProUGUI scoreUI;
public TextMeshProUGUI cookieUI;
public TextMeshProUGUI gameOverScoreUI;
void Start()
{
score = 0;
hp = 3;
cookiePower = 0;
}
void Update()
{
// 設定Score的計算方式
if(isGameOver == false)
{
score_f = score_f + Time.deltaTime;
score = Mathf.RoundToInt(score_f);
}
// 設定UI畫面中各項數值與文字的變化
hpUI.text = " HP: " + hp;
rabbitUI.text = " rabbits: " + rabbitPoint;
scoreUI.text = " Score: " + score;
gameOverScoreUI.text = scoreUI.text;
cookieUI.text = " cookie power: " + cookiePower;
}
public void updateHP(int amount)
{
hp = hp + amount;
if(hp >= 3)
{
hp = 3;
}
if(hp <= 0)
{
Debug.Log("遊戲結束");
GameOver();
}
}
public void updateCookiePower()
{
cookiePower = cookiePower + 10;
}
public void updateRabbitPoint()
{
rabbitPoint = rabbitPoint + 1;
}
public void ChangeStepUI(int step)
{
for (int i = 0; i < 3; i++)
{
steps[i].SetActive(false);
}
steps[step].SetActive(true);
}
public void GameStart()
{
SceneManager.LoadScene("SuperNewJeans");
}
public void Exit()
{
SceneManager.LoadScene("SuperNewJeans-Home");
}
public void GameOver()
{
// 遊戲結束
isGameOver = true;
// 判斷是否更新最高分
int oldScore = PlayerPrefs.GetInt("highScore");
int newScore = score;
if (newScore > oldScore)
{
PlayerPrefs.SetInt("highScore", newScore);
}
// 切換UI畫面
ChangeStepUI(2);
}
public void Restart()
{
SceneManager.LoadScene("SuperNewJeans-Home");
}
}
➔ 回到 Unity 介面,在 Game Manager 物件 中會發現剛剛新增的 UI 變數欄位
➔ 將 EndScore 物件拖曳至 Game Over Score UI 的欄位中
示意圖為 GameManager 腳本上半部修改後之程式碼
示意圖為 GameManager 腳本下半部修改後之程式碼
回到首頁完成最高分設定
➔ 開啟 SuperNewJeans-Home 場景 ➔ 開啟 HomeManager 腳本,將程式碼修改為以下版本並存檔
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using TMPro;
public class HomeManager : MonoBehaviour
{
public TextMeshProUGUI highScoreUI;
void Start()
{
highScoreUI.text = "HighScore:" + PlayerPrefs.GetInt("highScore");
}
void Update()
{
}
public void GameStart()
{
SceneManager.LoadScene("SuperNewJeans");
}
}
➔ 回到 Unity 介面,在 Game Manager 物件 中會發現剛剛新增的 UI 變數欄位
➔ 將 HighScore 物件拖曳至 High Score UI 的欄位中即可
示意圖為 HomeManager 腳本修改後之程式碼
當更新最高分紀錄後,便會自動更新首頁的 HighScore 數值